<?php

namespace App\Models;


use App\Models\Traits\HasDates;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;


/**
 * App\Models\Material
 *
 * @property int $id
 * @property int $uid
 * @property string|null $name
 * @property string $thumb
 * @property string $source
 * @property string|null $width
 * @property string|null $height
 * @property string|null $type
 * @property string|null $extension 扩展名
 * @property int $size
 * @property string|null $mime
 * @property int $views
 * @property int $downloads
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read int|string $formated_size
 * @property-read string $image
 * @property-read \App\Models\User $user
 * @method static Builder|Material doc()
 * @method static Builder|Material file()
 * @method static Builder|Material filter(array $input = [], $filter = null)
 * @method static Builder|Material image()
 * @method static Builder|Material newModelQuery()
 * @method static Builder|Material newQuery()
 * @method static Builder|Material paginateFilter($perPage = null, $columns = [], $pageName = 'page', $page = null)
 * @method static Builder|Material query()
 * @method static Builder|Material simplePaginateFilter(?int $perPage = null, ?int $columns = [], ?int $pageName = 'page', ?int $page = null)
 * @method static Builder|Material video()
 * @method static Builder|Material voice()
 * @method static Builder|Material whereBeginsWith(string $column, string $value, string $boolean = 'and')
 * @method static Builder|Material whereCreatedAt($value)
 * @method static Builder|Material whereDownloads($value)
 * @method static Builder|Material whereEndsWith(string $column, string $value, string $boolean = 'and')
 * @method static Builder|Material whereExtension($value)
 * @method static Builder|Material whereHeight($value)
 * @method static Builder|Material whereId($value)
 * @method static Builder|Material whereLike(string $column, string $value, string $boolean = 'and')
 * @method static Builder|Material whereMime($value)
 * @method static Builder|Material whereName($value)
 * @method static Builder|Material whereSize($value)
 * @method static Builder|Material whereSource($value)
 * @method static Builder|Material whereThumb($value)
 * @method static Builder|Material whereType($value)
 * @method static Builder|Material whereUid($value)
 * @method static Builder|Material whereUpdatedAt($value)
 * @method static Builder|Material whereViews($value)
 * @method static Builder|Material whereWidth($value)
 * @mixin \Eloquent
 */
class Material extends Model
{
    use Filterable, HasDates;

    protected $table = 'material';
    protected $primaryKey = 'id';
    protected $fillable = [
        'uid', 'name', 'source', 'thumb', 'width', 'height', 'type', 'extension', 'size', 'views', 'downloads', 'mime'
    ];
    protected $appends = [
        'formated_size',
        'image'
    ];
    protected $with = ['user'];

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::creating(function (Material $material) {
            if (!$material->uid) $material->uid = Auth::id();
        });

        static::deleting(function (Material $material) {
            Storage::delete($material->getRawOriginal('thumb'));
            Storage::delete($material->getRawOriginal('source'));
        });
    }

    /**
     * @param $value
     * @return string
     */
    public function getThumbAttribute($value)
    {
        if ($this->type == 'image') {
            return $value ? image_url($value) : $value;
        }

        if ($this->type == 'video') {
            return asset('images/common/video.png');
        }

        if ($this->type == 'voice') {
            return asset('images/common/audio.png');
        }

        if ($this->type == 'doc') {
            return asset('images/common/doc.png');
        }

        return asset('images/common/file.png');
    }

    /**
     * @param $value
     */
    public function setThumbAttribute($value)
    {
        $this->attributes['thumb'] = strip_image_url($value);
    }

    /**
     * @param $value
     * @return string
     */
    public function getImageAttribute()
    {
        return $this->source ? image_url($this->source) : null;
    }


    /**
     * @param $value
     * @return string
     */
    public function getSourceAttribute($value)
    {
        return $value ? material_url($value) : $value;
    }

    /**
     * @param $value
     */
    public function setSourceAttribute($value)
    {
        $this->attributes['source'] = strip_image_url($value);
    }

    /**
     * @return int|string
     */
    public function getFormatedSizeAttribute()
    {
        return $this->size ? format_size($this->size) : 0;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeImage(Builder $query)
    {
        return $query->where('type', 'image');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeVideo(Builder $query)
    {
        return $query->where('type', 'video');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeVoice(Builder $query)
    {
        return $query->where('type', 'voice');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeDoc(Builder $query)
    {
        return $query->where('type', 'doc');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeFile(Builder $query)
    {
        return $query->where('type', 'file');
    }
}
